import { _decorator, Collider, Component, ITriggerEvent, Node } from 'cc';
import { GameUtil } from './GameUtil';
import { Group } from './GameConstant';
import { GameManager } from './GameManager';
import { Barrier } from './Barrier';
import { PoolManager } from '../AshFramework/Managers/PoolManager';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('Target')
export class Target extends Barrier {

    @property(Collider)
    Collider1: Collider = null;

    @property(Collider)
    Collider2: Collider = null;

    @property(Collider)
    Collider3: Collider = null;

    @property(Node)
    Targets1: Node[] = [];

    @property(Node)
    Targets2: Node[] = [];

    @property(Node)
    Targets3: Node[] = [];

    // IsCollison: boolean = false;//是否发生碰撞

    protected onLoad(): void {
        this.Collider1.on(`onTriggerStay`, this.onTriggerStary1, this);
        this.Collider2.on(`onTriggerStay`, this.onTriggerStary2, this);
        this.Collider3.on(`onTriggerStay`, this.onTriggerStary3, this);
    }

    protected start(): void {
        this.Targets1 = GameUtil.shuffleArray(this.Targets1);
        this.Targets2 = GameUtil.shuffleArray(this.Targets2);
        this.Targets3 = GameUtil.shuffleArray(this.Targets3);
    }

    onTriggerStary1(event: ITriggerEvent) {
        if (event.otherCollider.getGroup() == Group.Bullet) {
            PoolManager.PutNode(event.otherCollider.node);
            this.strike(this.Targets1);
        }
    }

    onTriggerStary2(event: ITriggerEvent) {
        if (event.otherCollider.getGroup() == Group.Bullet) {
            PoolManager.PutNode(event.otherCollider.node);
            this.strike(this.Targets2);
        }
    }

    onTriggerStary3(event: ITriggerEvent) {
        if (event.otherCollider.getGroup() == Group.Bullet) {
            PoolManager.PutNode(event.otherCollider.node);
            this.strike(this.Targets3);
        }
    }

    strike(arrNode: Node[]) {
        if (arrNode.length == 0) return;
        AudioManager.PlaySound(Audios.HitTarget);
        GameManager.Instance.pushTarget(arrNode[0]);
        arrNode.splice(0, 1);
    }

}


